//
//  XDNetworkTool.swift
//  weibo
//
//  Created by lucy on 2016/10/25.
//  Copyright © 2016年 com.huashengHuaSheng. All rights reserved.
//

import UIKit
import AFNetworking

enum XDNetworkToolRequestMethod:String{

    case get = "get"
    case post = "post"
    
}

class XDNetworkTool: AFHTTPSessionManager {
    
    //网络请求单例
    static let shareTool: XDNetworkTool = {
    
        let tools = XDNetworkTool()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    
    }()
    
    
    /// 网络工具封装
    ///
    /// - parameter method:     请求方式
    /// - parameter urlStr:     地址
    /// - parameter parameters: 参数
    /// - parameter success:    成功回调
    /// - parameter failure:    失败回调
    func request(method:XDNetworkToolRequestMethod, urlStr:String,parameters:Any?, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        if method == .get {
            
            get(urlStr, parameters: parameters, progress: nil, success: { (_, responseObject) in
                
                success(responseObject)
                }, failure: { (_, error) in
                    
                failure(error)
            })
            
        }else{
        
            post(urlStr, parameters: parameters, progress: nil, success: { (_, responseObject) in
                success(responseObject)

                }, failure: { (_, error) in
                failure(error)

            })
       
        }
    }
}


//首页相关接口
extension XDNetworkTool{

    func homeLoadData(sinceId:Int64, maxId:Int64, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
    
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let param: [String:Any] = [
            "access_token": XDUserAccountViewModel.shareTools.accessToken!,
            "sinceId" : sinceId,
            "maxId" : maxId
        ]
        request(method: XDNetworkToolRequestMethod.get, urlStr: urlString, parameters: param, success: success, failure: failure)

    }
}


//OAuth授权相关接口
extension XDNetworkTool{
    //获取UserAccount
    func oauthloadUserAccount(code: String, success:@escaping (Any)->(), failure: @escaping(Error)->()){
    
        let url = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": XDAPPKEY,
            "client_secret": XDAPPSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": XDREDIRECTURI

        ]
        
        request(method: XDNetworkToolRequestMethod.post, urlStr: url, parameters: params, success: success, failure: failure)
    
    
    }
    
    //请求用户信息
    func oauthLoadUserInfo(model: XDUserAccountModel, success:@escaping (Any?)->(),failure:@escaping (Error)->()){
    
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        request(method: XDNetworkToolRequestMethod.get, urlStr: urlString, parameters: params, success: success, failure: failure)

    }
}

































